// https://www.bilibili.com/video/BV1d54y1f7B3?spm_id_from=333.788.videopod.episodes&vd_source=d6fe65f095f01f05591e89d810720ccc&p=30
// 2024-10-23

public class P032_井字游戏_分类 {

    public static void main(String[] args) {
        System.out.println(fun(new String[] {"xxx","oxo","o o"}));
    }

    private static boolean fun(String[] strings) {
        // x win x - o == 1 //x先下
        // y win x - o == 0
        // not people win x - o == 1 和 x - o == 0，都有可能

        int xCount = 0,oCount = 0;
        for (String row : strings) {
            for (char c : row.toCharArray()) {
                if (c == 'x') xCount++;
                if (c == 'o') oCount++;
            }
        }

        if (xCount != oCount && xCount - oCount != 1) return false;

        if (win(strings,"xxx") && xCount - oCount == 1) return true;
        if (win(strings,"ooo") && xCount - oCount == 0) return true;

        return false;
    }

    private static boolean win(String[] board,String flag) {
        for (int i = 0; i < 3; i++)
            if (flag.equals(board[i]) || flag.equals("" + board[0].charAt(i) + board[1].charAt(i) + board[2].charAt(i))) return true;
        return flag.equals("" + board[0].charAt(0) + board[1].charAt(1) + board[2].charAt(2))
                || flag.equals("" + board[0].charAt(2) + board[1].charAt(1) + board[2].charAt(0));



        //纵向3连
//            if (flag.equals("" + board[0].charAt(i) + board[1].charAt(i) + board[2].charAt(i))) return true;
            //横向3连
//            if (flag.equals(board[i])) return true;

            // 结合
        // 主对角线3连
//        if (flag.equals("" + board[0].charAt(0) + board[1].charAt(1) + board[2].charAt(2))) return true;
        // 副对角线3连
//        if (flag.equals("" + board[0].charAt(2) + board[1].charAt(1) + board[2].charAt(0))) return true;
//        return false;
    }
}
